<?php
/**
*baseclass.php 功能:vmi 基础类库
*@date:2017年12月08日
*@author:jiangshilin 
*/
class Baseclass
{
    protected $env;
    //初始化方法
    public function __construct(){
        
    }

    /**
     * curl提交数据
     * $data 提交数据  array()
     * 设置host主机头
     * $method
     * @return mixed
     */
    static function invoke_curl($url, $data, $httpHeader = array(),$method = 'POST', $timeout = 300)
    {
        $ch = curl_init();
        $timeout = $timeout <= 0 ? 300 : $timeout;
        $sendData['data'] = $data;
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);        
        if(strtoupper($method) == 'POST') {
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
        } else {//GET
            curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data));
        }
        //$httpHeader = array('Host:127.0.0.1');
        if(!empty($httpHeader) && is_array($httpHeader)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $handles = curl_exec($ch);
        $error = curl_error($ch);//排除错误
        curl_close($ch);
        return $handles;
    }

    /**
     * curl提交数据
     * $data 提交数据  array()
     * 设置host主机头
     * $method
     * @return mixed
     */
    static function invoke_curl_org($url, $data, $httpHeader = array(),$method = 'POST', $timeout = 300)
    {
        $ch = curl_init();
        $timeout = $timeout <= 0 ? 300 : $timeout;
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);        
        if(strtoupper($method) == 'POST') {
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else {//GET
            curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data));
        }
        //$httpHeader = array('Host:127.0.0.1');
        if(!empty($httpHeader) && is_array($httpHeader)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $handles = curl_exec($ch);
        $error = curl_error($ch);//排除错误
        curl_close($ch);
        return $handles;
    }
}